{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import argparse\n",
    "import json\n",
    "import shutil\n",
    "import sys\n",
    "import pickle\n",
    "\n",
    "import numpy as np\n",
    "import torch\n",
    "import skvideo.io\n",
    "\n",
    "from numpy.lib.format import open_memmap\n",
    "\n",
    "# sys.path.append(\n",
    "#     os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))\n",
    "sys.path.append(\n",
    "    os.path.abspath(\"..\"))\n",
    "import tools\n",
    "import tools.utils as utils\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "class Struct():\n",
    "    pass\n",
    "arg=Struct()\n",
    "arg.project_path=\"/home/jiangdong/workspace/st-gcn/\"\n",
    "arg.data_path=os.path.join(arg.project_path,\"output/Skating/\")\n",
    "part = ['train', 'val']\n",
    "for p in part:\n",
    "    json_file=os.path.join(arg.data_path,\"{}_label.json\".format(p))\n",
    "    new_json_file=os.path.join(arg.data_path,\"new_{}_label.json\".format(p))\n",
    "    with open(json_file,\"r\") as jf:\n",
    "        videos_info=json.load(jf)\n",
    "        new_json={}\n",
    "        target_dir=os.path.join(arg.data_path,\"{}_data\".format(p))\n",
    "        shutil.rmtree(target_dir, ignore_errors=True)\n",
    "        if(os.path.exists(target_dir)==False):\n",
    "            os.makedirs(target_dir)\n",
    "        for name,value in videos_info.items():\n",
    "            name=name.split('.')[0]\n",
    "            old_path=os.path.join(arg.data_path,\"data\",\"{}.json\".format(name))\n",
    "            new_json[name]=value\n",
    "#             print(old_path)\n",
    "            target_path=os.path.join(target_dir,\"{}.json\".format(name))\n",
    "#             print(target_path)\n",
    "            command=\"cp {} {}\".format(old_path,target_path)\n",
    "            os.system(command)\n",
    "        with open(new_json_file,'w') as f:\n",
    "            json.dump(new_json,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "class Struct():\n",
    "    pass\n",
    "arg=Struct()\n",
    "arg.project_path=\"/home/jiangdong/workspace/st-gcn/\"\n",
    "arg.data_path=os.path.join(arg.project_path,\"output/Skating/\")\n",
    "part = ['train', 'val']\n",
    "for p in part:\n",
    "    json_file=os.path.join(arg.data_path,\"{}_label.json\".format(p))\n",
    "    new_json_file=os.path.join(arg.data_path,\"new_{}_label.json\".format(p))\n",
    "    with open(json_file,\"r\") as jf:\n",
    "        videos_info=json.load(jf)\n",
    "        new_json={}\n",
    "        for name,value in videos_info.items():\n",
    "            name=name.split('.')[0]\n",
    "            new_json[name]=value\n",
    "        with open(new_json_file,'w') as f:\n",
    "            json.dump(new_json,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1326, 125139, 425, 294.44470588235293)\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "class Struct():\n",
    "    pass\n",
    "arg=Struct()\n",
    "arg.data_path=\"../data/Skating\"\n",
    "\n",
    "arg.trainfile=os.path.join(arg.data_path,\"train.csv\")\n",
    "arg.testfile=os.path.join(arg.data_path,\"val.csv\")\n",
    "arg.labelfile=os.path.join(arg.data_path,\"classInd.csv\")\n",
    "def _find_max(filename):\n",
    "    with open(filename) as f:\n",
    "        max_frames=0\n",
    "        sum_frames=0\n",
    "        count=0\n",
    "        for line in f.readlines():\n",
    "            info=line.strip()\n",
    "            video_name ,frames,label=info.split(\" \")\n",
    "            sum_frames+=int(frames)\n",
    "            count+=1\n",
    "            max_frames=max(int(frames),max_frames)\n",
    "        return max_frames,sum_frames,count,sum_frames/count\n",
    "print(_find_max(arg.testfile))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(989, 3, 1500, 18, 1)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy.io as io \n",
    "import os\n",
    "import pickle\n",
    "import pandas as pd\n",
    "class Struct():\n",
    "    pass\n",
    "arg=Struct()\n",
    "p=\"train\"\n",
    "arg.project_path=\"/home/jiangdong/workspace/st-gcn/\"\n",
    "arg.data_path=os.path.join(arg.project_path,\"output/Skating/\")\n",
    "datafile=os.path.join(arg.data_path,\"{}_data.npy\".format(p))\n",
    "labelfile=os.path.join(arg.data_path,\"{}_label.pkl\".format(p))\n",
    "matfile=os.path.join(arg.data_path,\"{}.mat\".format(p))\n",
    "with open(labelfile,'rb') as lf:\n",
    "    name,label=pickle.load(lf)\n",
    "data = np.load(datafile)\n",
    "# print(data)\n",
    "# print(label)\n",
    "io.savemat(matfile,{'data':data,'label':label,'name':name})\n",
    "print(data.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思路整理\n",
    "根据train.txt和val.txt获取每一个视频文件路径\n",
    "\n",
    "对每个视频\n",
    "\n",
    "    使用openpose生成预测文件\n",
    "    \n",
    "    将预测文件打包为数据和标签\n",
    "    \n",
    "    将数据和标签存入对应文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# class Struct():\n",
    "#     pass\n",
    "# arg=Struct()\n",
    "# arg.data_path=\"../data/Skating\"\n",
    "# arg.out_folder=\"../output/Skating\"\n",
    "\n",
    "# arg.trainfile=os.path.join(arg.data_path,\"train.csv\")\n",
    "# arg.testfile=os.path.join(arg.data_path,\"val.csv\")\n",
    "# arg.labelfile=os.path.join(arg.data_path,\"classInd.csv\")\n",
    "\n",
    "# arg.openpose=\"/home/jiangdong/opt/openpose/build\"\n",
    "# arg.model_folder=\"../models\"\n",
    "# openpose = '{}/examples/openpose/openpose.bin'.format(arg.openpose)\n",
    "\n",
    "# def _count_lines(filename):\n",
    "#     with open(filename) as f:\n",
    "#         count=-1\n",
    "#         for count,_ in enumerate(f):\n",
    "#             pass\n",
    "#         count+=1\n",
    "#     return count\n",
    "\n",
    "# def _video_loader(filename):\n",
    "#     with open(filename) as f:\n",
    "#         for line in f.readlines():\n",
    "#             info=line.strip()\n",
    "#             video_name , _,label=info.split(\" \")\n",
    "#             yield video_name,str(int(label)+1)\n",
    "\n",
    "# def pose_estimation(openpose,out_folder,video_path,model_folder,info):\n",
    "#     video_name=video_path.split('/')[-1].split('.')[0]\n",
    "#     output_snippets_dir=os.path.join(out_folder,'openpose_estimation/snippets/{}'.format(video_name))\n",
    "#     output_sequence_dir = os.path.join(out_folder,'data/')\n",
    "#     output_sequence_path = '{}/{}.json'.format(output_sequence_dir, video_name)\n",
    "#     # pose estimation\n",
    "#     openpose_args = dict(\n",
    "#         video=video_path,\n",
    "#         write_json=output_snippets_dir,\n",
    "#         display=0,\n",
    "#         render_pose=0, \n",
    "#         model_pose='COCO',\n",
    "#         model_folder=model_folder)\n",
    "#     command_line = openpose + ' '\n",
    "#     command_line += ' '.join(['--{} {}'.format(k, v) for k, v in openpose_args.items()])\n",
    "#     shutil.rmtree(output_snippets_dir, ignore_errors=True)\n",
    "#     os.makedirs(output_snippets_dir)\n",
    "#     print(command_line)\n",
    "#     os.system(command_line)\n",
    "#     # pack openpose ouputs\n",
    "#     video = utils.video.get_video_frames(video_path)\n",
    "#     height, width, _ = video[0].shape\n",
    "#     video_info = utils.openpose.json_pack(\n",
    "#         output_snippets_dir, video_name, width, height, label_index=info[\"label_index\"],label=info[\"label\"])\n",
    "#     if not os.path.exists(output_sequence_dir):\n",
    "#         os.makedirs(output_sequence_dir)\n",
    "#     with open(output_sequence_path, 'w') as outfile:\n",
    "#         json.dump(video_info, outfile)\n",
    "#     if len(video_info['data']) == 0:\n",
    "#         print('Can not find pose estimation results of %s'%(video_name))\n",
    "#         return\n",
    "#     else:\n",
    "#         print('%s Pose estimation complete.'%(video_name))    \n",
    "# print(os.getcwd())\n",
    "# label_names={}\n",
    "# with open(arg.labelfile) as lf:\n",
    "#     for line in lf.readlines():\n",
    "#         index,label_name=line.strip().split(\" \")\n",
    "#         label_names[index]=label_name\n",
    "# print(label_names)\n",
    "# part = ['train', 'val']\n",
    "# for p in part:\n",
    "#     csvfile=os.path.join(arg.data_path,\"{}.csv\".format(p))\n",
    "#     label_file={}\n",
    "#     total_count = _count_lines(csvfile)\n",
    "#     count=0\n",
    "#     for nameinfo,label in _video_loader(csvfile):\n",
    "#         try:\n",
    "#             filename=nameinfo.split('/')[3]+\".mp4\"\n",
    "#             category=filename.split(\"_\")[0]\n",
    "#             info={}\n",
    "#             info['label_index']=int(label)\n",
    "#             info['has_skeleton']=True\n",
    "#             info['label']=label_names[label]\n",
    "#             label_file[filename]=info\n",
    "#             video_path = os.path.join(arg.data_path,category,filename)\n",
    "#             pose_estimation(openpose,arg.out_folder,video_path,arg.model_folder,info)\n",
    "#             count+=1\n",
    "#             print(\"%4.2f %% of %s has been processed\"%(count*100/total_count,p))\n",
    "#         except Exception as e:\n",
    "#             print(e)\n",
    "#     label_save_path=os.path.join(arg.out_folder,\"{}_label.json\".format(p))\n",
    "#     with open(label_save_path,\"w\") as f:\n",
    "#         json.dump(label_file,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "line=\"/share/SkatingFlow/3Lutz_n28_p10_g04\"\n",
    "print(line.split('/')[3].split(\"_\")[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gcn",
   "language": "python",
   "name": "gcn"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
